﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Marbles.Core.Connectors;
using System.Data;

namespace Marbles.Core.Content.Links
{
    public class LinkCollection : ICollection<Link>
    {

        public int Count
        {
            get { return items.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public Link this[int index] { get { return items[index]; } set { items[index] = value; } }

        private List<Link> items = null;

        public LinkCollection()
        {
            items = new List<Link>();
        }

        public LinkCollection(string SiteId)
            : this()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM Links WHERE siteid = @siteid ORDER BY title ASC");
            cmd.AddParameter("@siteid", SiteId);
            foreach (DataRow row in dbc.FetchDataTable(cmd).Rows)
            {
                items.Add(new Link(row));
            }

        }

        public void Add(Link item)
        {
            items.Add(item);
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool Contains(Link item)
        {
            return items.Contains(item);
        }

        public void CopyTo(Link[] array, int arrayIndex)
        {
            items.CopyTo(array, arrayIndex);
        }

        public bool Remove(Link item)
        {
            return items.Remove(item);
        }

        public IEnumerator<Link> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return items.GetEnumerator();
        }
    }
}